package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout mainLayout = findViewById(R.id.mainLayout);
        mainLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Button btn = new Button(MainActivity.this);
                btn.setText("Hello");
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                        ScaleUtils.dip2px(MainActivity.this,80),
                        ScaleUtils.dip2px(MainActivity.this,40));
                mainLayout.addView(btn,lp);
            }
        });
    }
}

//    public class ScaleUtils {
//    public static int dip2px(Context context, float dpValue) {
//        final float scale = context.getResources().getDisplayMetrics().density;
//        return (int)(dpValue * scale + 0.5f);
//    }
//
//    public static int px2dip(Context context, int pxValue) {
//        return ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pxValue, context.getResources().getDisplayMetrics()));
//    }
//}
